<?php

class DefaultController extends PController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='main';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
		array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('view','login','team','last'),
				'users'=>array('*'),
		),
		array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('update','teamview','couponview','teamcouponview','use','used','expressview','express','logout'),
				'users'=>array('@'),
		),
		array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','create','index','delete'),
				'users'=>array('admin'),
		),
		array('deny',  // deny all users
				'users'=>array('*'),
		),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */


	public function actionLogin()
	{
		$model=new DefaultLoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['DefaultLoginForm']))
		{
			$model->attributes=$_POST['DefaultLoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(array('view?id='.Yii::app()->getComponent('partner')->id));
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}
		public function actionTeam()
	{
		 $nowtime=time();
		 $nowtime='end_time>='.$nowtime;
		 $dataProvider=new CActiveDataProvider('Team',array(
		 'criteria'=>array(
		 'condition'=>$nowtime,
		 'order'=>'begin_time DESC',
		 'with'=>array('city'),
		 ),
		 'pagination'=>array(
		    'pageSize'=>3,
		 ),
		));
		$this->render('team',array(
			'dataProvider'=>$dataProvider,
		));
	}
	public function actionLast()
	{
		 $nowtime=time();
		 $nowtime='end_time<'.$nowtime;
		$dataProvider=new CActiveDataProvider('Team',array(
		 'criteria'=>array(
		 'condition'=>$nowtime,
		 'order'=>'end_time DESC',
		 'with'=>array('city'),
		 ),
		 'pagination'=>array(
		    'pageSize'=>6,
		 ),
		));
		$this->render('last',array(
			'dataProvider'=>$dataProvider,
		));
	}
	public function actionLogout()
	{
		Yii::app()->getComponent('partner')->logout();
		$this->redirect(Yii::app()->homeUrl);
	}

	public function actionView($id)
	{
		if (Yii::app()->getComponent('partner')->id==$id)
		{
			$this->render('view',array(
			'model'=>$this->loadModel($id),
			));
		}
		else if(Yii::app()->user->name=='admin')
		{
			$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
		}
		else
		{
			$this->render('view',array(
			'model'=>$this->loadModel(Yii::app()->getComponent('partner')->id),
			));
		}
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Partner('register');

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Partner']))
		{
			$model->attributes=$_POST['Partner'];
			if($model->save())
			$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Partner']))
		{
			$model->attributes=$_POST['Partner'];
			if($model->save())
			$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'index' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
		throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Partner');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Partner('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Partner']))
		$model->attributes=$_GET['Partner'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Partner::model()->findByPk((int)$id);
		if($model===null)
		throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='partner-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	public function actionTeamview()
	{
		$id=Yii::app()->getComponent('partner')->id;
		$id='partner_id='.$id;
		$condition=$id." AND delivery='express'";
		$dataProvider=new CActiveDataProvider('Team',array(
		 'criteria'=>array(
		 'condition'=>$condition,
		 )));
		$this->render('teamview',array(

			'dataProvider'=>$dataProvider,
			));

	}
	public function actionCouponview()
	{
		$id=Yii::app()->getComponent('partner')->id;
		$id='partner_id='.$id;
		$dataProvider=new CActiveDataProvider('Coupon',array(
		 'criteria'=>array(
		 'condition'=>$id,
		 )));
		$this->render('couponview',array(

			'dataProvider'=>$dataProvider,
			));

	}
	public function actionTeamcouponview()
	{

		$id=$_GET['id'];
		$id='team_id='.$id;
		$dataProvider=new CActiveDataProvider('Coupon',array(
		 'criteria'=>array(
		 'condition'=>$id,
		 )));
		$this->render('teamcouponview',array(

			'dataProvider'=>$dataProvider,
			));

	}

	public function actionUse()
	{
    $model=new Coupon;

    // uncomment the following code to enable ajax-based validation
    /*
    if(isset($_POST['ajax']) && $_POST['ajax']==='coupon-use-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
    */

    if(isset($_POST['Coupon']))
    {
    	//print_r($_POST);
        $id=$_POST['Coupon']['id'];
        $secret=$_POST['Coupon']['secret'];
        //echo $id;
        $couponmodel=Coupon::model()->findByPk($id);
		$partnerid=$couponmodel->partner_id;
		$coupontime=$couponmodel->expire_time;
		$now=time();
        if($couponmodel->secret==$secret&&$couponmodel->consume=='N')
        {
        	if($couponmodel->consume=='N')
        	{
        		if($partnerid==Yii::app()->getComponent('partner')->id)
				{
					if($coupontime>=$now)
					{
						Coupon::model()->updateByPk((int)$id, array('consume'=>'Y'));
						Coupon::model()->updateByPk((int)$id, array('consume_time'=>$now));
						$message='消费成功！';
					}
					else $message='您的优惠券已过期！';
				}
				else $message='您的优惠券不属于本商户！';
        	}
        	else $message='此券已消费！';
        }
        else $message='没有此券！';

    }
    $this->render('use',array('model'=>$model,'message'=>$message));
	}

	public function actionExpressview()
	{
		$teamid=$_GET['id'];
		$teamid='team_id='.$teamid;
		$condition=$teamid." AND express='Y' AND state='pay' AND express_id=0";
		$dataProvider=new CActiveDataProvider('Order',array(
		 'criteria'=>array(
		 'condition'=>$condition,
		 )));
		$this->render('Expressview',array(

			'dataProvider'=>$dataProvider,
			));

	}
	public function actionExpress()
	{
		$oid=$_GET['id'];
    	$model=new Order;
    	if(isset($_POST['Order']))
           {//	print_r($_POST);
        	$express=$_POST['Order']['express_xx'];
        	$no=$_POST['Order']['express_no'];
        	if($express&&$no)
        		{
        			Order::model()->updateByPk((int)$oid, array('express_xx'=>$express,
        														'express_id'=>'1',
        														'express_no'=>$no,
        														));
        			$message='发货成功';
        		}
           }
      $this->render('express',array('model'=>$model,'message'=>$message));
	}


		public function loadcouponModel($id)
	{
		$model=Coupon::model()->findByPk((int)$id);
		if($model===null)
		throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

}
